<?php
/*
 * 注册处理
 * @arrange Mark
 * @package ixdcw
 * @file    register.inc.php
 * @time    12-12-19 下午12:02
 */
defined('IN_IXDCW') or exit('Access Denied');
// 已经登录
if($_userid) dheader(SITE_URL);
require DT_CORE.'/'.$module.'/common.inc.php';
require_once MD_ROOT.'/member.class.php';

// 打印协议
if(isset($print)) {
	include template('agreement', $module);
	exit();
}

// 是否开启注册
if(!$MOD['enable_register']) Prompt::message($L['register_msg_close'], SITE_URL);
// 是否防卫代理注册
if($MOD['defend_proxy']) {
	if($_SERVER['HTTP_X_FORWARDED_FOR'] || $_SERVER['HTTP_VIA'] || $_SERVER['HTTP_PROXY_CONNECTION'] || $_SERVER['HTTP_USER_AGENT_VIA'] || $_SERVER['HTTP_CACHE_INFO']) {
		Prompt::message(lang('include->defend_proxy'));
	}
}
// 注册客户端屏蔽
if($MOD['banagent']) {
	$banagent = explode('|', $MOD['banagent']);
	foreach($banagent as $v) {
		if(strpos($_SERVER['HTTP_USER_AGENT'], $v) !== false) Prompt::message($L['register_msg_agent'], SITE_URL, 5);
	}
}
// IP注册间隔限制(小时)
if($MOD['iptimeout']) {
	$timeout = $DT_TIME - $MOD['iptimeout']*3600;
	$r = $db->getOne("SELECT userid FROM {$db->getPre()}member WHERE regip='$DT_IP' AND regtime>'$timeout'");
	if($r) Prompt::message(lang($L['register_msg_ip'], array($MOD['iptimeout'])), SITE_URL);
}
// 是否开启邮件发送 && 是否邮件验证 否则是不验证用户的
if($DT['mail_type'] == 'close' && $MOD['checkuser'] == 2) $MOD['checkuser'] = 0;
require_once DT_INC.'/post.func.php';
$do = new member();
// 开启session
Session::start();
// 是否开启注册邮件验证码
// mail_type 是否关闭邮件发送
$could_emailcode = ($MOD['emailcode_register'] && $DT['mail_type'] != 'close');
if($could_emailcode && $MOD['checkuser'] == 2) $MOD['checkuser'] = 0;

$action_sendcode = Crypt::action('sendcode');
if($action == $action_sendcode) {
	$email = isset($value) ? trim($value) : '';
	if(!is_email($email)) exit('2');
	if($do->email_exists($email)) exit('3');
	if(!$do->is_email($email)) exit('4');
	$emailcode = Str::random(6, '0123456789');
	$_SESSION['email'] = $email;
	$_SESSION['email_code'] = md5($email.'|'.$emailcode);
	$title = $L['register_msg_emailcode'];
	$content = ob_template('emailcode', 'mail');
	send_mail($email, $title, stripslashes($content));
	exit('1');
}

$CFD = PCache::read('fields-company.php');

$GROUP = PCache::read('group.php');
if($post) {

	//if($action != crypt_action('register')) Prompt::alert($L['check_sign']);
	$post['passport'] = isset($post['passport']) && $post['passport'] ? $post['passport'] : $post['username'];

	if($MOD['passport'] == 'uc') {
		$passport = Str::convert($post['passport'], DT_CHARSET, $MOD['uc_charset']);
		require DT_ROOT.'/api/uc.inc.php';
		list($uid, $rt_username, $rt_password, $rt_email) = uc_user_login($passport, $post['password']);
		if($uid == -2) Prompt::alert($L['register_msg_passport'], '', 'parent.Dd("passport").focus();');
	}

	$msg = captcha($captcha, $MOD['captcha_register'], true);
	if($msg) Prompt::alert($msg);
	$msg = question($answer, $MOD['question_register'], true);
	if($msg) Prompt::alert($msg);
	//if($_SESSION['regemail'] != md5(md5($post['email'].DT_KEY.$DT_IP))) Prompt::alert($L['check_sign']);
	$reload_captcha = $MOD['captcha_register'] ? reload_captcha() : '';
	$reload_question = $MOD['question_register'] ? reload_question() : '';
	// 默认个人会员
	$post['regid'] = 5;
	if($could_emailcode) {
		if(!preg_match("/[0-9]{6}/", $post['emailcode']) || $_SESSION['email_code'] != md5($post['email'].'|'.$post['emailcode'])) Prompt::alert($L['register_pass_emailcode'], '', $reload_captcha.$reload_question);
	}
	if($post['regid'] == 5) $post['company'] = $post['truename'];
	$post['groupid'] = $MOD['checkuser'] ? 4 : $post['regid'];
	$post['content'] = $post['introduce'] = $post['thumb'] = $post['banner'] = $post['catid'] = $post['catids'] = '';
	$post['edittime'] = 0;
	$inviter = Cookie::get('inviter');
	$post['inviter'] = $inviter ? Crypt::decode($inviter) : '';

	if($do->add($post)) {
		$userid = $do->userid;
		$username = $post['username'];
		$email = $post['email'];

		if($MOD['passport'] == 'uc') {
			$uid = uc_user_register($passport, $post['password'], $post['email']);
		}

		//send sms
		if($MOD['welcome_sms'] && $DT['sms'] && is_mobile($post['mobile'])) {
			$message = lang('sms->wel_reg', array($post['truename'], $DT['sitename'], $post['username'], $post['password']));
			$message = strip_sms($message);
			send_sms($post['mobile'], $message);
		}
		//send sms
		if($MOD['checkuser'] == 2) {
			$auth = make_auth($username);
			$db->query("UPDATE {$db->getPre()}member SET auth='$auth',authvalue='$email',authtime='$DT_TIME' WHERE username='$username'");
			$authurl = linkurl($MOD['linkurl'], 1).'send.php?action=check&auth='.$auth;
			$title = $L['register_msg_activate'];
			$content = ob_template('check', 'mail');
			send_mail($email, $title, $content);
			$goto = $MOD['linkurl'].'goto.php?action=register&email='.$email;
			Prompt::alert('', '', 'top.window.location="'.$goto.'";');
		} else if($MOD['checkuser'] == 0) {
			if($MOD['welcome_message'] || $MOD['welcome_email']) {
				$title = $L['register_msg_welcome'];
				$content = ob_template('welcome', 'mail');
				if($MOD['welcome_message']) send_message($username, $title, $content);
				if($MOD['welcome_email'] && $DT['mail_type'] != 'close') send_mail($post['email'], $title, $content);
			}
		}
		if($could_emailcode) $db->query("UPDATE {$db->getPre()}member SET vemail=1 WHERE username='$username'");
		session_destroy();
		echo '<html><head><title>Login...</title><meta http-equiv="Content-Type" content="text/html;charset='.DT_CHARSET.'"></head>';
		echo '<body onload="document.getElementById(\'login\').submit();">';
		echo '<form method="post" action="'.$MOD['linkurl'].'user_login.php" id="login" target="_top">';
		echo '<input type="hidden" name="forward" value="'.($forward ? $forward : $MOD['linkurl']).'"/>';
		echo '<input type="hidden" name="username" value="'.$username.'"/>';
		echo '<input type="hidden" name="goto" value="1"/>';
		echo '<input type="hidden" name="password" value="'.$post['password'].'"/>';
		echo '<input type="hidden" name="auto" value="1"/>';
		echo '<input type="hidden" name="captcha" value=""/>';
		echo '</form></body></html>';
		exit;
	} else {
		$reload_captcha = $MOD['captcha_register'] ? reload_captcha() : '';
		$reload_question = $MOD['question_register'] ? reload_question() : '';
		Prompt::alert($do->errmsg, '', $reload_captcha.$reload_question);
	}
} else {
	$COM_TYPE = explode('|', $MOD['com_type']);
	$COM_SIZE = explode('|', $MOD['com_size']);
	$COM_MODE = explode('|', $MOD['com_mode']);
	$MONEY_UNIT = explode('|', $MOD['money_unit']);
	$mode_check = dcheckbox($COM_MODE, 'post[mode][]', '', 'onclick="check_mode(this);"', 0);
	$auth = isset($auth) ? rawurldecode($auth) : '';
	$username = $password = $email = $passport = '';
	if($auth) {
		$auth = Crypt::decode($auth);
		$auth = explode('|', $auth);
		$passport = $auth[0];
		if(User::checkname($passport)) $username = $passport;
		$password = $auth[1];
		$email = is_email($auth[2]) ? $auth[2] : '';
		if($email) $_SESSION['regemail'] = md5(md5($email.DT_KEY.$DT_IP));
	}
	$areaid = $cityid;
	Cookie::set('forward_url', $forward);
	$head_title = $L['register_title'];
	include template('register', $module);
}
?>